package com.jjc.android.dialog

import android.content.Context
import android.text.SpannableStringBuilder
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.allen.library.SuperButton
import com.jjc.android.R
import com.jjc.android.util.dp2px
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils

class MessageInfoDialog(private val builder: Builder) : CenterPopupView(builder.context) {

    private lateinit var cancleBtn: SuperButton
    private lateinit var agreemnetText: TextView
    private lateinit var closeBtn: ImageView
    private lateinit var titleText:TextView
    private lateinit var okBtn: SuperButton
    override fun getImplLayoutId(): Int {
        return if(!builder.btnText.isNullOrEmpty()&&!builder.okText.isNullOrEmpty()) R.layout.dialog_message_info_two_btn else R.layout.dialog_message_info
    }

    override fun onCreate() {
        super.onCreate()
        closeBtn = findViewById(R.id.closeBtn)
        agreemnetText = findViewById(R.id.agreemnetText)
        cancleBtn = findViewById(R.id.cancleBtn)
        okBtn = findViewById(R.id.okBtn)
        titleText = findViewById(R.id.titleText)
        closeBtn.visibility = View.GONE
        closeBtn.setOnClickListener {
            dismiss()
        }
        val bottomSpace:View = findViewById(R.id.bottomSpace)
        val scrollView:View = findViewById(R.id.scrollView)
        scrollView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{
            override fun onGlobalLayout() {
                (scrollView.layoutParams as? ConstraintLayout.LayoutParams)?.let {
                    val maxHeight = (XPopupUtils.getScreenHeight(context)*0.8f).toInt()
                    val top = titleText.height+titleText.top+16.dp2px
                    val bottom = (scrollView.parent as View).height-bottomSpace.bottom
                    it.matchConstraintMaxHeight = maxHeight-top-bottom
                    scrollView.layoutParams = it
                }
                scrollView.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })

        titleText.text = builder.title
        agreemnetText.text = if(builder.contentSpan!=null) builder.contentSpan else builder.content
        if(builder.btnText.isNullOrEmpty()){
            cancleBtn.visibility = View.GONE
        }else{
            cancleBtn.visibility = View.VISIBLE
            cancleBtn.text = builder.btnText
            cancleBtn.setOnClickListener {
                builder.btnAction?.invoke()
                dismiss()
            }
        }
        if(builder.okText.isNullOrEmpty()){
            okBtn.visibility = View.GONE
        }else{
            okBtn.visibility = View.VISIBLE
            okBtn.text = builder.okText
            okBtn.setOnClickListener {
                builder.okAction?.invoke()
                dismiss()
            }
        }
    }

    override fun getPopupWidth(): Int {
        return (XPopupUtils.getScreenWidth(context)*0.9f).toInt()
    }

    override fun getPopupHeight(): Int {
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context)*0.8f).toInt()
    }

    class Builder(val context: Context) {
        var btnAction:(()->Unit)? = null
        var title:String? = null
        var content:String = ""
        var contentSpan: SpannableStringBuilder? = null
        var btnText:String? = null
        var okText:String? = null
        var okAction:(()->Unit)? = null

        fun title(title:String): Builder {
            this.title = title
            return this
        }

        fun content(title:String): Builder {
            content = title
            return this
        }

        fun contentSpan(contentSpan:SpannableStringBuilder): Builder{
            this.contentSpan = contentSpan;
            return this
        }

        fun btnText(okText:String):Builder{
            this.btnText = okText
            return this
        }

        fun btnClick(action:()->Unit):Builder{
            btnAction = action
            return this
        }

        fun okText(okText:String):Builder{
            this.okText = okText
            return this
        }

        fun okClick(action:()->Unit):Builder{
            okAction = action
            return this
        }

        fun build():MessageInfoDialog{
            return MessageInfoDialog(this).apply {
                XPopup.Builder(context).dismissOnBackPressed(false).dismissOnTouchOutside(false).asCustom(this)
            }

        }
    }
}